import tkinter as tk
from tkinter import messagebox

# 注册新用户
def register_user():
    username = username_entry.get()
    password = password_entry.get()

    # 检查用户名和密码是否为空
    if username == "" or password == "":
        messagebox.showerror("错误", "用户名和密码不能为空")
        return

    # 写入用户名和密码到文件
    with open('zh.txt', 'a') as f:
        f.write("{},{}\n".format(username, password))

    messagebox.showinfo("成功", "注册成功！请使用您的新账号登录")

    # 清空输入框
    username_entry.delete(0, 'end')
    password_entry.delete(0, 'end')


# 创建注册窗口
window = tk.Tk()
window.title("注册界面")

# 设置窗口大小和位置
window.geometry("1000x800")
window.resizable(False, False)

# 创建背景图片标签
background_image = tk.PhotoImage(file="background.jpg")
background_label = tk.Label(window, image=background_image)
background_label.place(x=0, y=0, relwidth=1, relheight=1)

# 创建用户名标签和输入框
username_label = tk.Label(window, text="用户名:", font=("Courier", 12, "bold"), bg="#e6e6e6")
username_label.place(relx=0.3, rely=0.3)
username_entry = tk.Entry(window, font=("Courier", 12), bg="#f2f2f2", width=20)
username_entry.place(relx=0.4, rely=0.3)

# 创建密码标签和输入框
password_label = tk.Label(window, text="密码:", font=("Courier", 12, "bold"), bg="#e6e6e6")
password_label.place(relx=0.3, rely=0.5)
password_entry = tk.Entry(window, show="*", font=("Courier", 12), bg="#f2f2f2")
password_entry.place(relx=0.4, rely=0.5)

# 创建注册按钮
register_button = tk.Button(window, text="注册", command=register_user, font=("Courier", 12, "bold"), bg="#4d4d4d",
                            fg="#ffffff")
register_button.place(relx=0.5, rely=0.7, anchor="center")

# 运行窗口主循环
window.mainloop()
